let option = {}; //配置项
let xAxis = {},
  yAxis = {}; //线性，柱形的x轴，y周
let series = []; //数据
let dataLabel = {
  //每条数据的文字展示位置，颜色
  label: {
    show: false,
    position: "top",
    formatter: function (parms) {
      return parms.name + ":" + parms.data;
    },
    textStyle: {
      color: "#fff",
    },
  },
};
let tooltip1 = {
  //鼠标经过提示框的样式
  trigger: "item",
  backgroundColor: "rgba(255,255,255,0.7)",
  textStyle: {
    color: "#666",
  },
  formatter: "{a} <br/>{b}: {c}",
};
let tooltip2 = {
  trigger: "axis",
  axisPointer: {
    // 坐标轴指示器，坐标轴触发有效
    type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
  },
};
let axisLabel = {
  // 坐标轴文字大于4个字这行显示
  formatter: function (params) {
    var newParamsName = ""; // 最终拼接成的字符串
    var paramsNameNumber = params.length; // 实际标签的个数
    var provideNumber = 4; // 每行能显示的字的个数
    var rowNumber = Math.ceil(paramsNameNumber / provideNumber); // 换行的话，需要显示几行，向上取整
    /**
     * 判断标签的个数是否大于规定的个数， 如果大于，则进行换行处理 如果不大于，即等于或小于，就返回原标签
     */
    // 条件等同于rowNumber>1
    if (paramsNameNumber > provideNumber) {
      /** 循环每一行,p表示行 */
      for (var p = 0; p < rowNumber; p++) {
        var tempStr = ""; // 表示每一次截取的字符串
        var start = p * provideNumber; // 开始截取的位置
        var end = start + provideNumber; // 结束截取的位置
        // 此处特殊处理最后一行的索引值
        if (p == rowNumber - 1) {
          // 最后一次不换行
          tempStr = params.substring(start, paramsNameNumber);
        } else {
          // 每一次拼接字符串并换行
          tempStr = params.substring(start, end) + "\n";
        }
        newParamsName += tempStr; // 最终拼成的字符串
      }
    } else {
      // 将旧标签的值赋给新标签
      newParamsName = params;
    }
    //将最终的字符串返回
    return newParamsName;
  },
};
let toolbox = {
  show: false,
  feature: {
    mark: { show: true },
    dataView: { show: true, readOnly: false },
    magicType: { type: ["line", "bar"] },
    restore: { show: true },
    saveAsImage: { show: true },
  },
};

let legend = {
  show: true,
  bottom: "0",
  left: "center",
  textStyle: {
    color: "#fff",
  },
  width: 400,
  height: 500,
  data: [],
};
let title = {
  show: true,
  text: "",
  left: "center",
  top: "0",
  textStyle: {
    color: "#000",
    fontSize: 16,
  },
};
/**
 * 1. pie》1饼形，2环形，3玫瑰，4环形玫瑰
 * 2. line》1折线，2圆滑弧线，3堆叠折线，4堆叠弧线，5背景折线，6渐变背景折线
 * 3. bar》1横向柱形，2竖向柱形，3极坐标系，4堆叠柱形
 * 4. pie/bar 折线柱形混用
 *
 *
 *
 * model  图形编号
 * data.legendData   图标数据数据
 * data.xAxisData    x轴数据
 * data.itemData     显示数据
 * grid   图在画布的位置
 * colors.color 数据图显示数据文字的颜色，
 * colors.theme 主题色 （主要是坐标轴颜色），
 * colors.itemColor 每条数据的颜色
 * colors.bgColor 背景色
 * legend.bool 是否数据显示图标
 * legend.bottom 数据显示图标在下方位置
 * legend.color 数据显示图标在左方位置
 * title  数据图标题
 * pie.radius 饼形图大小，传数组为环形图
 * pie.center 饼形图位置 []
 * labelStyle.bool 是否显示每条数据的提示信息
 * labelStyle.position 每条数据信息的显示位置
 * labelStyle.seriesNmae 每条数据的name
 * toolboxShow 是否显示现在工具
 * **/

function bar(
  model,
  data,
  grid,
  colors,
  legends,
  tit,
  Axis,
  labelStyle,
  toolboxShow
) {
  series = [];
  let type = "bar"; // 类型
  let stack = ""; // 堆叠
  let name = [];
  let num = [];
  let values = [],
    msgDate = [];
  let tooltip = {};
  console.log("type--------",model);
  tooltip = tooltip2;
  if (data.itemData[0] instanceof Array) {
    data.itemData.forEach((item, index) => {
      values = [];
      item.forEach((it) => {
        values.push(it["num"]);
      });
      msgDate.push({
        data: values,
      });
    });
  } else {
    data.itemData.map((item) => {
      num.push(item.num);
    });
  }
  xAxis = Axis.xAxis;
  xAxis.data = data.xAxisData;
  xAxis.boundaryGap = true;
  yAxis = Axis.yAxis;
  if (model === "a") {
    xAxis = Axis.yAxis;
    xAxis.data = data.xAxisData;
    yAxis = Axis.xAxis;
  } else if (model === "b") {
  } else if (model === "c") {
  } else if (model === "d") {
    // xAxis=Axis.yAxis
    // xAxis.data=data.xAxisData
    // xAxis.boundaryGap=true
    dataLabel.label.position = labelStyle.position;
    xAxis = {
      show: false,
      type: "value",
      // name: '水量',
      axisLabel: {
        show: false,
      },
      splitLine: {
        show: false,
      },
      axisLine: {
        show: false,
      },
      interval: 50,
    };
    yAxis = {
      data: data.xAxisData,
    };
    stack = "堆";
  }
  // 标题
  title.text = tit;
  // legend显示位置/样式
  legend.show = legends.bool;
  legend.left = legends.left;
  legend.bottom = legends.bottom;
  legend.textStyle.color = legends.color;
  legend.data = data.legendData;
  // 柱形图数据的文字显示
  toolbox.show = toolboxShow;
  dataLabel.label.show = labelStyle.bool;
  dataLabel.label.position = labelStyle.position;
  dataLabel.label.textStyle.color = colors.color;
  dataLabel.label.formatter = labelStyle.formatter;
  if (model === "lineOrBar") {
    msgDate.map((item, index) => {
      series.push({
        name: labelStyle.seriesNmae[index],
        type: index === 0 ? "line" : "bar",
        stack: stack,
        data: item.data,
        label: dataLabel.label,
        itemStyle: {
          color: colors.bgColor[index],
        },
      });
    });
  } else {
    if (msgDate.length !== 0) {
      msgDate.map((item, index) => {
        series.push({
          name: labelStyle.seriesNmae[index],
          type: type,
          stack: stack,
          data: item.data,
          label: dataLabel.label,
          itemStyle: {
            color: colors.bgColor[index],
          },
        });
      });
    } else {
      series.push({
        name: labelStyle.seriesNmae[0],
        type: type,
        stack: stack,
        data: num,
        label: dataLabel.label,
        itemStyle: {
          color: colors.itemColor,
        },
      });
    }
  }
  option = {
    title: title,
    toolbox: toolbox,
    tooltip: tooltip,
    legend: legend,
    grid: grid,
    xAxis: xAxis,
    yAxis: yAxis,
    series: series,
  };
  console.log(option);
  return option;
}
function line(
  model,
  data,
  grid,
  colors,
  legends,
  tit,
  Axis,
  labelStyle,
  toolboxShow
) {
  //折线图：标准折线、圆滑折线、堆叠折线、堆叠弧线、背景折线、渐变折线
  series = [];
  let type = "line";
  let smooth = false; //弧形线、折线
  let stack = ""; // 堆叠
  let num = [];
  let bgColor;
  let values = [],
    msgDate = [],
    tooltip = {};
  xAxis = Axis.xAxis;
  yAxis = Axis.yAxis;
  xAxis.boundaryGap = false;
  xAxis.data = data.xAxisData;
  // data操作
  xAxis.axisLabel = axisLabel;
  tooltip = tooltip2;
  if (data.itemData[0] instanceof Array) {
    data.itemData.forEach((item, index) => {
      values = [];
      item.forEach((it) => {
        values.push(it["num"]);
      });
      msgDate.push({
        data: values,
      });
    });
  } else {
    data.itemData.map((item) => {
      num.push(item.num);
    });
  }
  // data
  if (model === "01") {
  } else if (model === "02") {
    smooth = true;
  } else if (model === "03") {
    stack = "堆";
  } else if (model === "04") {
    stack = "堆";
    smooth = true;
  } else if (model === "05") {
    bgColor = {
      color: colors.bgColor[0],
    };
  } else if (model === "06") {
    bgColor = {
      color: {
        type: "linear",
        x: 0, // 渐变方向
        y: 0, // 渐变方向
        x2: 0, // 渐变方向
        y2: 1, // 渐变方向
        colorStops: [
          {
            offset: 0,
            color: colors.bgColor[0],
          },
          {
            offset: 1,
            color: colors.bgColor[1],
          },
        ],
      },
    };
  }
  // 标题
  title.text = tit;
  // legend显示位置 、样式
  legend.show = legends.bool;
  legend.left = legends.left;
  legend.bottom = legends.bottom;
  legend.textStyle.color = legends.color;
  legend.data = data.legendData;
  // 折线图的工具栏
  toolbox.show = toolboxShow;
  // 折线图数据的文字显示 位置、颜色
  dataLabel.label.show = labelStyle.bool;
  dataLabel.label.position = labelStyle.position;
  dataLabel.label.textStyle.color = colors.color;
  dataLabel.label.formatter = labelStyle.formatter;
  if (msgDate.length !== 0) {
    msgDate.map((item, index) => {
      series.push({
        name: labelStyle.seriesNmae[index],
        type: type,
        stack: stack,
        smooth: smooth,
        areaStyle: bgColor,
        data: item.data,
        label: dataLabel.label,
        itemStyle: {
          color: colors.bgColor[index],
        },
      });
    });
  } else {
    series.push({
      name: labelStyle.seriesNmae[0],
      type: type,
      stack: stack,
      smooth: smooth,
      areaStyle: bgColor,
      data: num,
      label: dataLabel.label,
      itemStyle: {
        color: colors.itemColor,
      },
    });
  }
  option = {
    title: title,
    tooltip: tooltip,
    toolbox: toolbox,
    legend: legend,
    grid: grid,
    xAxis: xAxis,
    yAxis: yAxis,
    series: series,
  };
  return option;
}
const proxyData = {
		get:(target,key)=>{
			return target[key]
		},
}

function pie(model, data, colors, legends, tit, pie, labelStyle, toolboxShow) {
	
  series = [];
  let datas = [],
  tooltip = {};
  let type = "pie";
  let roseType = ""; //玫瑰图
  const itemData = JSON.parse(JSON.stringify(data.itemData))
  datas = itemData.map((o,index) => {
	console.log(o)
    legend.data.push(o[index].name);
    return { name: o[index].name, value: o[index].num };
  });
  if (model === "1") {
    pie.radius = "60%";
  } else if (model === "2") {
    pie.radius = ["30%", "70%"];
  } else if (model === "3") {
    pie.radius = "60%";
    roseType = "radius";
  } else if (model === "4") {
    pie.radius = ["30%", "70%"];
    roseType = "radius";
  }
  tooltip = tooltip1;
  // 标题
  title.text = tit;
  // legend显示位置 、样式
  legend.show = legends.bool;
  legend.left = legends.left;
  legend.bottom = legends.bottom;
  legend.textStyle.color = legends.color;
  // 饼形工具栏
  toolbox.show = toolboxShow;
  // 饼形图数据的文字显示位置，样式
  dataLabel.label.show = labelStyle.bool;
  dataLabel.label.position = labelStyle.position; //inside、center
  dataLabel.label.textStyle.color = labelStyle.color;
  // 饼形图数据显示标题与数据
  dataLabel.label.formatter = labelStyle.formatter;
  series.push({
    name: labelStyle.seriesNmae[0],
    type: type,
    data: datas,
    radius: pie.radius,
    roseType: roseType,
    center: pie.center,
    label: dataLabel.label,
  });
  option = {
    title: title,
    tooltip: tooltip,
    toolbox: toolbox,
    legend: legend,
    series: series,
  };
}
export function chart(type, dataMsg) {
  if (type === "pie") {
    pie(
      dataMsg.model,
      dataMsg.data,
      dataMsg.colors,
      dataMsg.legend,
      dataMsg.title,
      dataMsg.pie,
      dataMsg.labelStyle,
      dataMsg.toolboxShow
    );
  } else if (type === "line") {
    line(
      dataMsg.model,
      dataMsg.data,
      dataMsg.grid,
      dataMsg.colors,
      dataMsg.legend,
      dataMsg.title,
      dataMsg.Axis,
      dataMsg.labelStyle,
      dataMsg.toolboxShow
    );
  } else if (type === "bar") {
    bar(
      dataMsg.model,
      dataMsg.data,
      dataMsg.grid,
      dataMsg.colors,
      dataMsg.legend,
      dataMsg.title,
      dataMsg.Axis,
      dataMsg.labelStyle,
      dataMsg.toolboxShow
    );
  } else if (type === "lineOrBar") {
    bar(
      type,
      dataMsg.data,
      dataMsg.grid,
      dataMsg.colors,
      dataMsg.legend,
      dataMsg.title,
      dataMsg.Axis,
      dataMsg.labelStyle,
      dataMsg.toolboxShow
    );
  }
  return option;
}
